package com.jeesite.modules.kj.api.wechatUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * <p>时间处理工具类</p></br>
 * author : Crab2Died</br>
 * date : 2017/5/23  10:35</br>
 */
public class DateUtils {

    static public final String DATE_FORMAT_DAY = "yyyy-MM-dd";
    static public final String DATE_FORMAT_DAY_2 = "yyyy/MM/dd";
    static public final String TIME_FORMAT_SEC = "HH:mm:ss";
    static public final String DATE_FORMAT_SEC = "yyyy-MM-dd HH:mm:ss";
    static public final String DATE_FORMAT_MIN = "yyyy-MM-dd HH:mm";
    static public final String DATE_FORMAT_MSEC = "yyyy-MM-dd HH:mm:ss.SSS";
    static public final String DATE_FORMAT_MSEC_T = "yyyy-MM-dd'T'HH:mm:ss.SSS";
    static public final String DATE_FORMAT_MSEC_T_Z = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";

    /**
     * 匹配yyyy-MM-dd
     */
    private static final String DATE_REG = "^[1-9]\\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$";
    /**
     * 匹配yyyy/MM/dd
     */
    private static final String DATE_REG_2 = "^[1-9]\\d{3}/(0[1-9]|1[0-2])/(0[1-9]|[1-2][0-9]|3[0-1])$";
    /**
     * 匹配HH:mm:ss
     */
    private static final String TIME_SEC_REG = "^(20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d$";
    /**
     * 匹配yyyy-MM-dd HH:mm:ss
     */
    private static final String DATE_TIME_REG = "^[1-9]\\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])\\s" +
            "(20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d$";
    /**
     * 匹配yyyy-MM-dd HH:mm:ss.SSS
     */
    private static final String DATE_TIME_MSEC_REG = "^[1-9]\\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])\\s" +
            "(20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d\\.\\d{3}$";
    /**
     * 匹配yyyy-MM-dd'T'HH:mm:ss.SSS
     */
    private static final String DATE_TIME_MSEC_T_REG = "^[1-9]\\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])T" +
            "(20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d\\.\\d{3}$";
    /**
     * 匹配yyyy-MM-dd'T'HH:mm:ss.SSS'Z'
     */
    private static final String DATE_TIME_MSEC_T_Z_REG = "^[1-9]\\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])T" +
            "(20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d\\.\\d{3}Z$";

    /**
     * <p>将{@link Date}类型转换为指定格式的字符串</p></br>
     * author : Crab2Died</br>
     * date   : 2017年06月02日  15:32:04</br>
     *
     * @param date   {@link Date}类型的时间
     * @param format 指定格式化类型
     * @return 返回格式化后的时间字符串
     */
    static
    public String date2Str(Date date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    /**
     * <p>将{@link Date}类型转换为默认为[yyyy-MM-dd HH:mm:ss]类型的字符串</p></br>
     * author : Crab2Died</br>
     * date   : 2017年06月02日  15:30:01</br>
     *
     * @param date {@link Date}类型的时间
     * @return 返回格式化后的时间字符串
     */
    static
    public String date2Str(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_SEC);
        return sdf.format(date);
    }

    /**
     * <p>根据给出的格式化类型将时间字符串转为{@link Date}类型</p></br>
     * author : Crab2Died</br>
     * date   : 2017年06月02日  15:27:22</br>
     *
     * @param strDate 时间字符串
     * @param format  格式化类型
     * @return 返回{@link java.util.Date}类型
     */
    static
    public Date str2Date(String strDate, String format) {
        Date date = null;
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            date = sdf.parse(strDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 秒转时分
     * */
    public static String getGapTime(int time) {
        int h = time / 3600;
        int m = (time % 3600) / 60;
        return h + "小时" + m + "分";
    }

    /***
     * @comments 计算两个时间的时间差
     * @param strTime1
     * @param strTime2
     */
    private String getTimeDifference(String strTime1, String strTime2) {

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date now = df.parse(strTime1);
            Date date = df.parse(strTime2);
            long l = now.getTime() - date.getTime();       //获取时间差
            long day = l / (24 * 60 * 60 * 1000);
            long hour = (l / (60 * 60 * 1000) - day * 24);
            long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
            long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
            return day + "天" + hour + "小时" + min + "分" + s + "秒";
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /***
     * @comments 当前时间与目标时间差天数
     * @param date
     * @return
     */
    public static long getDateDifference(Date date) {

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date now = new Date();
            long l = now.getTime() - date.getTime();
            long day = l / (24 * 60 * 60 * 1000);
            return day;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    /***
     * @comments 当前时间与目标时间差天数
     * @param date
     * @return
     */
    public static long gethourDifference(Date date) {

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date now = new Date();
            long l = now.getTime() - date.getTime();
            long hour = (l / (60 * 60 * 1000));
            return hour;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    /***
     * @comments 计算两个时间的时间差, 小时，分
     * @param strTime1
     * @param strTime2
     */
    public static String getTimeDifferenceHour(String strTime1, String strTime2) {

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date now = df.parse(strTime1);
            Date date = df.parse(strTime2);
            long l = now.getTime() - date.getTime();       //获取时间差
            long hour = (l / (60 * 60 * 1000));
            long min = ((l / (60 * 1000)) - hour * 60);
            return hour + "小时" + min + "分";
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取过去第几天的日期
     * @param past 2018-01-01
     * @return 2018-01-01 ~ 2018-01-31
     * @author ThinkGem
     */
    public static Date getPastDate(int past) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
        Date pastDate = calendar.getTime();
        return pastDate;
    }
}
